package com.lbd.dynamicCode.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.dynamicCode.bean.MsgBatch;
import com.lbd.dynamicCode.service.BatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * Created by zzl on 2017/11/19.
 */
@RestController
@RequestMapping("/batch")
public class BatchController extends BaseController {
    @Autowired
    private BatchService batchService;

    /**
     * 按条件分页查询批次信息
     *
     * @param page  第几页
     * @param rows  一页多少条
     * @param batch
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(@RequestParam(name = "page", required = false, defaultValue = "1") int page,
                       @RequestParam(name = "rows", required = false, defaultValue = "10") int rows,MsgBatch batch) {
        return new Result(ResultState.SUCCESS, "查询成功",batchService.findByPageConditionJpql(page, rows, batch));
    }

    /**
     * 保存批次信息
     *
     * @param batch
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(MsgBatch batch, HttpSession session) {
        batchService.save(batch);
        return new Result(ResultState.SUCCESS, "添加成功", "");
    }

    /**
     * 根据id查询回显要编辑的Batch
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", batchService.findById(id));
    }

    /**
     * 修改batch信息
     *
     * @param batch
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(MsgBatch batch) {
        batchService.update(batch);
        return new Result(ResultState.SUCCESS, "更新成功");
    }

    /**
     * 删除批次信息
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        batchService.del(ids);
        return new Result(ResultState.SUCCESS, "删除成功", "");

    }

}
